﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.DB;
using YT.Modules;
using YT.Commons;

namespace YT.XH.MeniereDisease
{
    public partial class frmMDTheraputicMethodDetail : Form
    {
        private int mintPeriod = 0;
        private string mstrSickRecID = string.Empty;
        private const string mstrCaseType = "间歇期";
        public delegate void change(string strPeriod);
        public event change onchange;

        private void Test(string strPeriod)
        {
            if (onchange != null)
            {
                onchange(strPeriod);
            }
        } 



        public frmMDTheraputicMethodDetail( int intPeriod, string strSickRecID)
        {
            InitializeComponent();
            mintPeriod = intPeriod;
            mstrSickRecID = strSickRecID;
        }

        private void FillOperation()
        {
            string strSQL = string.Empty;
            strSQL = "select top 1 * from mTreatHistory where Period='" + mintPeriod + "' and SickRecID='"+ mstrSickRecID  +"' and CaseType='"+ mstrCaseType +"' order by recid desc ";
            DBAccess dbcon = new DBAccess(false, false);
            DataRow drRow = dbcon.GetDataFirstRow(strSQL);
            if (drRow == null )
            {
                initOperationName();
                return; 
            }
            initOperationName();
            string strOperationName= CvtFunc.ToString ( drRow ["TreatCaseName"]);
            GetRadFromOperationName(mintPeriod, strOperationName);
        }

        private void initOperationName()
        {
            string strSQL = string.Empty;

            int intAge = 0;
            bool blnTwo = false;
            bool blnIsInclude = false;
            bool blnBeyondOneYear = false;
            //检查年龄
            strSQL = "select * from mSickPerson where SickRecID='"+ mstrSickRecID +"' ";
            DBAccess dbcon = new DBAccess(false, false);
            DataRow drAge = dbcon.GetDataFirstRow(strSQL);
            if (drAge != null) 
            {
                intAge = CvtFunc.ToInt(drAge["Age"]);
                blnIsInclude = CvtFunc.ToBoolean(drAge["IsInclude"]);
                blnBeyondOneYear = CvtFunc.ToBoolean(drAge["BeyondOneYear"]);
            }

            //治疗前检查的“口服甘油前” 0.5K, 1K,2K,3K 的气导算术平均值。
            strSQL = " SELECT SUM(CAST(Result AS int)) FROM ExamBeforTreatResult WHERE (HdrSeqNo = '22') " +
                          " AND (DtlSeqNo IN ('2', '3', '4', '5', '9', '10', '11', '12')) AND " +
                          " (RowNo = 'B') and SickRecID='"+ mstrSickRecID  +"' and Type='"+ TitleType.MeniereDisease  +"' and isnumeric(Result) = 1 ";
            double dblAmt = dbcon.GetDoubleValue(strSQL);
            dblAmt =　dblAmt /8.00　;
            
            //双侧判断
            strSQL = "select * from ExamBeforTreatResult where SickRecID='"+ mstrSickRecID +"' and HdrSeqNo='107' and DtlSeqNo='1' and RowNo='C'";
            DataRow drTwo = dbcon.GetDataFirstRow(strSQL );
            if (drTwo != null)
                blnTwo = CvtFunc.ToBoolean(drTwo["Result"]);
            else
                blnTwo = false;

            //有无仪器
            string strCurHospitalID = DataFunctions.GetFieldValue("mSickPerson", "HospitalID", "SickRecID='" + mstrSickRecID + "'");
            bool blnHasYQ = CvtFunc.ToBoolean ( DataFunctions.GetFieldValue ( "mHospital","HasMeniett","  HospitalID='"+ strCurHospitalID +"'") );

            if (blnHasYQ)
            {
                if ( (intAge > 70 || intAge < 18) || blnTwo )
                {
                    rad方案2.Enabled = false;
                    rad方案3.Enabled = false;
                }
            }
            else
            {
                if ((intAge > 70 || intAge < 18) || blnTwo)
                {
                    rad方案2.Enabled = false;
                    rad方案3.Enabled = false;

                    rad方案5.Enabled = false;                    
                }
                else
                {
                    rad方案5.Enabled = false;                    
                }
            }
            if (dblAmt >= 40)
            {
                rad方案1.Enabled = false;
            }
            else
            {
                rad方案2.Enabled = false;
                rad方案3.Enabled = false; 
            }

            lblSelCase.Text = "";
            string strDesc = string.Empty;
            if (intAge > 70 || intAge < 18)
            {
                strDesc = "此患者年龄是 " + intAge + " 周岁， ";
            }
            if (blnTwo)
            {
                strDesc = " 双侧 梅尼埃， ";
            }
            strDesc += " PTA 值： " + dblAmt + " db";

            if (blnHasYQ)            
                strDesc += "\r\n" + " 有Meniett仪器 ";
            else
                strDesc += "\r\n" + " 无Meniett仪器 ";

            if (blnIsInclude)
            {
                if (blnBeyondOneYear)
                {
                    rad方案1.Enabled=false ;
                }
                else
                {
                    rad方案2.Enabled = false;
                    rad方案3.Enabled = false;
                }
            }

            lblSelCase.Text = strDesc;
            
            strSQL = "select top 1 * from mTreatHistory where Period='" + mintPeriod + "' and SickRecID='" + mstrSickRecID + "' and CaseType='" + mstrCaseType + "' order by recid desc ";            
            DataRow drRow = dbcon.GetDataFirstRow(strSQL);
            if (drRow != null) return;
            if (mintPeriod > 1) return;
            ArrayList arrName = new ArrayList();
            if (rad方案1.Enabled) arrName.Add(1);
            if (rad方案2.Enabled) arrName.Add(2);
            if (rad方案3.Enabled) arrName.Add(3);
            if (rad方案4.Enabled) arrName.Add(4);
            if (rad方案5.Enabled) arrName.Add(5);            

            int intMaxRecID =5;
            Random rad = new Random();
            int intRad = 0;
            while (!arrName.Contains(intRad))
            {
                intRad = rad.Next(1, intMaxRecID);
            }
            if (intRad == 1) rad方案1.Checked = true;
            if (intRad == 2) rad方案2.Checked = true;
            if (intRad == 3) rad方案3.Checked = true;
            if (intRad == 4) rad方案4.Checked = true;
            if (intRad == 5) rad方案5.Checked = true;            
        }

        private void GetRadFromOperationName(int intPeriod, string strOperationName)
        {           
            if (strOperationName == "方案1") rad方案1.Checked = true;
            if (strOperationName == "方案2") rad方案2.Checked = true;
            if (strOperationName == "方案3") rad方案3.Checked = true;
            if (strOperationName == "方案4") rad方案4.Checked = true;
            if (strOperationName == "方案5") rad方案5.Checked = true;            
        }
        private string GetOperationNameFromRad()
        {
            if (rad方案1.Checked) return "方案1";
            if (rad方案2.Checked) return "方案2";
            if (rad方案3.Checked) return "方案3";
            if (rad方案4.Checked) return "方案4";
            if (rad方案5.Checked) return "方案5";            
            return "";
        }
        public bool UpdateTheraputicMethodDetail()
        {
            string strTreatCaseName = GetOperationNameFromRad();
            DBAccess dbcon =new DBAccess (false,false );
            if (strTreatCaseName == "") return true;
            string strSQL = "if not exists ( select top 1 * from mTreatHistory where SickRecID='" + mstrSickRecID + "'  and TreatCaseName='" + strTreatCaseName + "' and CaseType='" + mstrCaseType + "' and Period='" + mintPeriod + "' and recid in (select max(recid) from mTreatHistory where SickRecID='" + mstrSickRecID + "' and Period='"+ mintPeriod +"' ) order by Period)"
                            + " Insert into mTreatHistory( SickRecID,TreatCaseName,Period,CaseType ) "
                            + " values ('" + mstrSickRecID + "','" + strTreatCaseName + "','" + mintPeriod + "','" + mstrCaseType + "') ";
                            //+ " else " 
                            //+ " update mTreatHistory set TreatCaseName ='"+ strTreatCaseName  +"' where Period='"+  +mintPeriod +"' and SickRecID='"+ mstrSickRecID  +"'"; 
                            dbcon.ExecuteSql(strSQL);

                            return true;
        }

        private void frmMDTheraputicMethodDetail_Load(object sender, EventArgs e)
        {
            FillOperation();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            UpdateTheraputicMethodDetail();
            if ( radContinue .Checked )
                Test(mintPeriod.ToString ());
            else if (radEnd.Checked)
            {
                DBAccess dbcon = new DBAccess(false, false);
                string strSQL = string.Empty;
                strSQL = "update mSickPerson set Finished=1 where SickRecID='" + mstrSickRecID + "'";
                dbcon.ExecuteSql(strSQL);
                dbcon.CloseCon();                         
            }

        }
    }
}
